﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;

namespace EHTWebsiteProject.Controls
{
    public partial class MenuLeftControl : System.Web.UI.UserControl
    {
        private Int32 DanhMucSanPhamID = HangSo.DEFAULT_ID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    DanhMucSanPhamID = Int32.Parse(Request.QueryString["DanhMucID"].ToString());
                }
                catch
                { 
                    
                }

                LoadMenus(DanhMucSanPhamID.ToString());
            }
        }


        /// <summary>
        /// Author : chiennv        
        /// 20/7/2012
        /// Description : Phương thức tạo menu
        /// </summary>
        public void LoadMenus(String psMenuID)
        {
            DataTable dtCategorys = DanhMucSanPhamProvider.GetDanhMucSanPhams(-1,"","").Tables[0];
            RadPanelBar1.ExpandMode = Telerik.Web.UI.PanelBarExpandMode.SingleExpandedItem;
            // Duyet mang danh muc san pham
            foreach (DataRow dr in dtCategorys.Select("ChuyenMucChaID = 0"))
            {
                //Menu cap 1
                // kiem tra xem no co la danh muc cha co chua danh muc con hay ko?
                // La danh muc co chua danh muc con
                if (HelpMethod.ToStr(dr["ChuyenMucChaID"]).Equals("0"))
                {
                    Telerik.Web.UI.RadPanelItem oItem1 = new Telerik.Web.UI.RadPanelItem(dr["TenDanhMucVN"].ToString());
                    oItem1.Font.Name = "Arial";
                    oItem1.Font.Bold = true;
                    oItem1.Expanded = false;
                    oItem1.Font.Size = 8;
                    ////kiem tra xem danh muc co duoc chon hay khong de view
                    DanhMucSanPham theCategory = new DanhMucSanPham();
                    try
                    {
                        theCategory = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(psMenuID));
                    }
                    catch
                    {
                        theCategory = null;
                    }
                    if (theCategory != null)
                    {
                        if (dr["DanhMucID"].ToString() == psMenuID || theCategory.ChuyenMucChaID.ToString() == dr["DanhMucID"].ToString())
                        {
                            oItem1.Expanded = true;
                            oItem1.ForeColor = System.Drawing.Color.FromName("#ff6600");
                        }
                    }                   
                    //Menu cap 2
                    //kiem tra xem danh muc nay co chua danh muc con hay ko
                    //neu la co chu danh muc con
                    if (dtCategorys.Select("ChuyenMucChaID = '" + dr["DanhMucID"].ToString() + "'", "SapXep ASC").LongLength > 0)
                    {
                        oItem1.NavigateUrl = "../Kategorier-" + dr["DanhMucID"].ToString() + ".html";

                        //duyet cac pham tu cua menu cap 1
                        foreach (DataRow dr1 in dtCategorys.Select("ChuyenMucChaID = '" + dr["DanhMucID"].ToString() + "'", "SapXep ASC"))
                        {
                            Telerik.Web.UI.RadPanelItem oItem2 = new Telerik.Web.UI.RadPanelItem(dr1["TenDanhMucVN"].ToString());
                            oItem2.Font.Name = "Arial";
                            oItem2.Font.Bold = true;
                            oItem2.ForeColor = System.Drawing.Color.FromName("#333");
                            oItem2.Font.Size = 8;

                            DanhMucSanPham theChild = new DanhMucSanPham();
                            try
                            {
                                theChild = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(psMenuID));
                            }
                            catch
                            {
                                theChild = null;
                            }
                            if (theChild != null)
                            {
                                if (dr1["DanhMucID"].ToString() == psMenuID || theChild.ChuyenMucChaID.ToString() == dr1["DanhMucID"].ToString())
                                {
                                    oItem2.Expanded = true;
                                    oItem2.ForeColor = System.Drawing.Color.FromName("#ff6600");                                   
                                }
                            }                           
                                                      
                            //Kiem tra xem menu cap 2 co chua menu con hay khong
                            //menu cap 2 co menu con
                            //Menu cap 3
                            if (dtCategorys.Select("ChuyenMucChaID = '" + dr1["DanhMucID"].ToString() + "'", "SapXep ASC").LongLength > 0)
                            {
                                oItem2.NavigateUrl = "../Kategorier-" + dr1["DanhMucID"].ToString() + ".html";

                                foreach (DataRow dr2 in dtCategorys.Select("ChuyenMucChaID = '" + dr1["DanhMucID"].ToString() + "'", "SapXep ASC"))
                                {
                                    Telerik.Web.UI.RadPanelItem oItem3 = new Telerik.Web.UI.RadPanelItem(dr2["TenDanhMucVN"].ToString());
                                    oItem3.Font.Name = "Arial";
                                    oItem3.Font.Bold = true;
                                    oItem3.ForeColor = System.Drawing.Color.FromName("#333");
                                    oItem3.NavigateUrl = "../Produkter-" + dr2["DanhMucID"].ToString() + ".html";

                                    oItem2.Items.Add(oItem3);

                                    //////////////////////////////////////////////////
                                    DanhMucSanPham theChild2 = new DanhMucSanPham();
                                    try
                                    {
                                        theChild2 = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(psMenuID));
                                    }
                                    catch
                                    {
                                        theChild2 = null;
                                    }
                                    if (theChild2 != null)
                                    {
                                        if (dr2["DanhMucID"].ToString() == psMenuID || theChild2.ChuyenMucChaID.ToString() == dr2["DanhMucID"].ToString())
                                        {

                                            oItem3.Expanded = true;
                                            oItem3.ForeColor = System.Drawing.Color.FromName("#ff6600");
                                            oItem1.ForeColor = System.Drawing.Color.FromName("#ff6600");
                                            oItem1.Expanded = true;
                                            oItem2.Expanded = true;
                                            oItem3.Font.Size = 8;
                                        }
                                    }

                                   //////////////////////////////////////////////
                                }                               
                            }
                            else
                            {
                                oItem2.NavigateUrl = "../Produkter-" + dr1["DanhMucID"].ToString() + ".html"; 
                            }

                            oItem1.Items.Add(oItem2);  
                        }
                    }

                    //khong chua danh muc con
                    else
                    {
                        oItem1.NavigateUrl = "../Produkter-" + dr["DanhMucID"].ToString() + ".html";
                    }
                   
                    RadPanelBar1.Items.Add(oItem1);

                }

            }

        }

    }
}